/**
 * Build menus from JSON. This class is used internal
 */

MenuItemId = {
	taoHD : 'taoHd',
	suaHD : 'suaHd',
	suaHDDenHang : 'suaHDDenHang',
	suaHDTraHang : 'suaHDTraHang',
	timHD : 'timHd',
	thongKeHD : 'thongKeHd',
	nhapPT : 'nhapPt',
	giaoPT : 'giaoPt',
	congSo : 'congSo',
	chamCong : 'chamCong',
	taoNV : 'taoNV',
	suaNV : 'suaNV',
	qlDuLieu : 'qlDuLieu'
};

var MenuMapping = {};
MenuMapping[MenuItemId.taoHD] = 'TaoHopDongPanel';
MenuMapping[MenuItemId.suaHD] = 'TaoHopDongPanel';
MenuMapping[MenuItemId.suaHDDenHang] = 'SuaHDDenHangPanel';
MenuMapping[MenuItemId.suaHDTraHang] = 'SuaHDTraHangPanel';
MenuMapping[MenuItemId.timHD] = 'TimHopDongPanel';
MenuMapping[MenuItemId.thongKeHD] = 'ThongKePanel';
MenuMapping[MenuItemId.nhapPT] = 'NhapPhieuThuPanel';
MenuMapping[MenuItemId.giaoPT] = 'GiaoPhieuThuPanel';
MenuMapping[MenuItemId.congSo] = 'CongSoThuNganPanel';
MenuMapping[MenuItemId.chamCong] = 'ChamCongPanel';
MenuMapping[MenuItemId.taoNV] = 'TaoNhanVienPanel';
MenuMapping[MenuItemId.suaNV] = 'SuaNhanVienPanel';
MenuMapping[MenuItemId.qlDuLieu] = 'TaoDuLieuPanel';

MenuBar = function() {

	var menubar = null;
	return {

		currentMenu : null, // Id of current selected menu item.
		/**
		 * The container of the menu
		 */
		menuContainer : null,

		getContainer : function() {
			return MenuBar.menuContainer;
		},

		/**
		 * create an instance of the menus's container
		 */
		init : function() {
			menubar = new Ext.Toolbar();
			var panel = new Ext.Panel({
				header : false,
				layout : 'fit',
				border : false,
				items : menubar
			});

			MenuBar.menuContainer = new Ext.Container({
				region : 'north',
				margins : '0 0 0 0',
				layout : 'card',
				height : 25,
				activeItem : 0,
				items : [ panel ]
			});
			return MenuBar.menuContainer;
		},

		buildMenu : function() {
			var items = [ {
				text : lang.text.menu.taoHd,
				id : MenuItemId.taoHD
			}, {
				text : lang.text.menu.suaHd,
				id : MenuItemId.suaHD
			}, {
				text : lang.text.menu.timHd,
				id : MenuItemId.timHD
			}, {
				text : lang.text.menu.thongKeHd,
				id : MenuItemId.thongKeHD
			}, {
				text : lang.text.menu.nhapPt,
				id : MenuItemId.nhapPT
			}, {
				text : lang.text.menu.giaoPt,
				id : MenuItemId.giaoPT
			}, {
				text : lang.text.menu.congSo,
				id : MenuItemId.congSo
			}, {
				text : lang.text.menu.chamCong,
				id : MenuItemId.chamCong
			}, {
				text : lang.text.menu.taoNv,
				id : MenuItemId.taoNV
			}, {
				text : lang.text.menu.suaNv,
				id : MenuItemId.suaNV
			}, {
				text : lang.text.menu.qlDuLieu,
				id : MenuItemId.qlDuLieu,
				endMenu : true
			} ];

			// Add menubar
			for ( var i = 0; i < items.length; i++) {
				menubar.add(new Ext.Button({
					id : items[i].id,
					text : items[i].text,
					handler : this.processChangeMenuClick
				}));
				
				if(!items[i].endMenu){
					menubar.add('-');
				}
			}
			
			// Add logout user link.
			if(ApplicationManager.currentUser){
				menubar.add({xtype: 'tbfill'});
				menubar.addText(ApplicationManager.currentUser);
				menubar.add('-', {
            		  xtype: 'box',
            		  autoEl: {tag: 'a', href: './user/logout', html: lang.text.logout}
            		});
			}
			
			MenuBar.getContainer().doLayout(false, true);
		},

		/**
		 * Process when click any menu item.
		 */
		processChangeMenuClick : function() {
			MenuBar.setActiveMenu(this.id);
		},

		setActiveMenu : function(menuId) {
			if (MenuBar.currentMenu != menuId || menuId == MenuItemId.taoHD) {
				ApplicationManager.openContentPage(menuId);
				MenuBar.currentMenu = menuId;
			}
		}
	}
}();